package cn.car.biz.carAccess.support.impl;

import cn.car.biz.carAccess.support.abs.AbstractSupport;
import cn.car.domains.ctx.PlateDetectedContext;
import cn.car.domains.entity.VehicleCommonInfoEntity;
import cn.car.domains.enums.VehicleTypeEnum;
import cn.car.exception.CarAccessControlException;
import cn.car.service.config.SystemConfig;
import cn.hutool.core.date.DateUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j
public class CommonSupport extends AbstractSupport {

    @Resource
    private SystemConfig systemConfig;

    @Override
    public String supportName() {
        return VehicleTypeEnum.COMMUTE.name();
    }

    @Override
    public void processIn(PlateDetectedContext context) {

        super.processIn(context);

        VehicleCommonInfoEntity vehicleCommonInfoEntity = context.getVehicleCommonInfoEntity();
        Date expireTime = vehicleCommonInfoEntity.getExpireTime();

        if (expireTime != null && expireTime.before(new Date())) {
            throw CarAccessControlException.SYSTEM_ERROR.newInstance("车辆已过期,过期时间:" + DateUtil.formatDateTime(expireTime));
        }
    }

    @Override
    public void manualRecordIn(PlateDetectedContext context) {
        VehicleCommonInfoEntity vehicleCommonInfoEntity = context.getVehicleCommonInfoEntity();
        context.setManualOperator("manual");
        if (vehicleCommonInfoEntity == null) {
            VehicleCommonInfoEntity commonInfoEntity = new VehicleCommonInfoEntity();
            commonInfoEntity.setVehicleNumber(context.getVehicleInfo().getPlateNumber());
            commonInfoEntity.setVehicleType(VehicleTypeEnum.COMMUTE.name());
            context.setVehicleCommonInfoEntity(commonInfoEntity);
            super.recordIn(context);
        }

        //当前排班拉出来 获取今天值班的人
        super.recordIn(context);
    }

    @Override
    public void manualRecordOut(PlateDetectedContext context) {
        VehicleCommonInfoEntity vehicleCommonInfoEntity = context.getVehicleCommonInfoEntity();
        context.setManualOperator("manual");
        if (vehicleCommonInfoEntity == null) {
            VehicleCommonInfoEntity commonInfoEntity = new VehicleCommonInfoEntity();
            commonInfoEntity.setVehicleNumber(context.getVehicleInfo().getPlateNumber());
            commonInfoEntity.setVehicleType(VehicleTypeEnum.COMMUTE.name());
            context.setVehicleCommonInfoEntity(commonInfoEntity);
            super.recordOut(context);
        }

        //当前排班拉出来 获取今天值班的人
        super.recordOut(context);
    }


}
